package me.linczh.framework.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class FrameworkFilterChain implements FilterChain {
	private List<Filter> chain;
	private FilterChain parentChain;
	private int index;
	
	public FrameworkFilterChain(FilterChain _chain) {
		index = 0;
		parentChain = _chain;
		chain = new ArrayList<Filter>();
		chain.add(new URLPatternFilter());
		chain.add(new ActionDispatcherFilter());
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		if (index < chain.size()) chain.get(index++).doFilter(request, response, this);
		else parentChain.doFilter(request, response);
	}

}
